#include "WidgetVariantColor.h"
#include <QColorDialog> 
#include "EVec4.h"
#include "ui_WidgetVariantColor.h"

WidgetVariantColor::WidgetVariantColor(QWidget *parent) :
    WidgetVariant(parent),
    ui(new Ui::WidgetVariantColor)
{
    ui->setupUi(this);
}

WidgetVariantColor::~WidgetVariantColor()
{
    delete ui;
}

void WidgetVariantColor::refreshByProperty( EItemMetaPropertyPtr p)
{
    WidgetVariant::refreshByProperty(p);
    WidgetVariantColor::clearConnect();

    EString text = p->nameCN.isEmpty() ? p->name : p->nameCN;
    if(text.left(3) == "ui_")
    {
        text.removeLeft(3);
    }
    ui->labelName->setText(text.data());

    EColor colorVariant = p->value.to_EColor();
    //set lineEdit's str
    QString colorText = "rgba=(";
    colorText += QString::number(colorVariant.r)+","
        +QString::number(colorVariant.g)+","
        +QString::number(colorVariant.b)+","
        +QString::number(colorVariant.a)+")";

    ui->lineEdit->setText(colorText);

    //colorVariant to colorQt
    QColor colorQt;
    colorQt.setRedF(colorVariant.r);
    colorQt.setGreenF(colorVariant.g);
    colorQt.setBlueF(colorVariant.b);
    colorQt.setAlphaF(colorVariant.a);

    //set lineEdit's background color
    QString ss = "rgb(";
    ss += QString::number(colorQt.red())+","
        +QString::number(colorQt.green())+","
        +QString::number(colorQt.blue())+","
        +QString::number(colorQt.alpha())+")";

    ui->lineEdit->setStyleSheet(QString("background-color: ")+ss);

    WidgetVariantColor::buildConnect();

}

void WidgetVariantColor::buildConnect()
{
	QObject::connect(ui->buttonSelect, SIGNAL(clicked(bool)), this, SLOT(slotButtonClicked(bool)));
}

void WidgetVariantColor::clearConnect()
{
	QObject::disconnect(ui->buttonSelect, SIGNAL(clicked(bool)), this, SLOT(slotButtonClicked(bool)));
}

void WidgetVariantColor::slotButtonClicked(bool clicked)
{
    EColor colorVariant = this->m_property->value.to_EColor();
    //colorVariant to colorQt
    QColor colorQt;
    colorQt.setRedF(colorVariant.r);
    colorQt.setGreenF(colorVariant.g);
    colorQt.setBlueF(colorVariant.b);
    colorQt.setAlphaF(colorVariant.a);
    
    colorQt = QColorDialog::getColor(colorQt, this, "select a color");  
  
    if (colorQt.isValid()) 
    {  
        //set lineEdit's background
        QString ss = "rgb(";
        ss += QString::number(colorQt.red())+","
            +QString::number(colorQt.green())+","
            +QString::number(colorQt.blue())+","
            +QString::number(colorQt.alpha())+")";

        ui->lineEdit->setStyleSheet(QString("background-color: ")+ss);

        //colorQt to colorVariant
        colorVariant.r = colorQt.redF();
        colorVariant.g = colorQt.greenF();
        colorVariant.b = colorQt.blueF();
        colorVariant.a = colorQt.alphaF();

        //set lineEdit's string
        QString colorText = "rgba=(";
        colorText += QString::number(colorVariant.r)+","
            +QString::number(colorVariant.g)+","
            +QString::number(colorVariant.b)+","
            +QString::number(colorVariant.a)+")";

        ui->lineEdit->setText(colorText);

        //update property
        EVariant varOld = this->m_property->value;
        EVariant var(colorVariant);
        this->m_property->setPropertyValue(var);

        emit signalVariantChanged(this->m_property, varOld);
    }  
}
